﻿// $(function(){
	// $("#noticeTab>ul li").each(function(i){//i获取匹配对象的索引
        // $(this).click(function(){//添加鼠标移入事件
            // $(".con:not(:eq("+i+"))").hide();//隐藏其它内容块
            // $(".con:eq("+i+")").show();//显示当前的内容块
            // $("#noticeTab>ul li").removeClass("active");
            // $(this).addClass("active");
        // });
    // });//为每一个对象应用函数
// });



var Query = RichBase.extend({
	statics : {
		currentPage : 1,
		last : "",
		queryState : {
			loading : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">加载数据...</p><img src="images/other/loading_1.gif" alt="" /></li>',
			empty : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">没有数据...</p></li>',
			fail : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求失败...</p></li>',
			unlogin : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">登录过期，请重新登录...</p></li>',
			timeout : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">网络请求超时...</p></li>',
			serverError : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求出错...</p></li>'
		},
		isObjEmpty : function(obj){
			for(var i in obj){
				return false;
			}
			return true;
		}
	}, 
	EVENTS : {
		"click" : {
			"#pagenavW .pageBtn" : "onPrevNextClick"
		}
	},
	init : function(opt){
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
		function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]); return null;
		}
		$(".info_url").attr("href",'userinfo.html?id='+getQueryString("?"))
		$("#did").val(getQueryString("?"));
		$(".credit_active").attr("aid",getQueryString("?"))
		
	},
	onPrevNextClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		if($("#prevPageBtn").attr("data-first")!=0){
			that.prevBtn.removeClass("disable");
		}else{
			that.prevBtn.addClass("disable");
		}
		var title = $(".searchInp").val();
		if(tarBtn.hasClass("disable")) return false;
		var curPage = $(".page_num_").val();
		var toPage = tarBtn.hasClass("prevBtn") ? (curPage-1) : (curPage+1);
		var all = $(".nextBtn").attr("total");
		var aid = $(".credit_active").attr("aid");
		var mid = Number($("#prevPageBtn").attr("data-mid"));
		var last_ = Number($("#prevPageBtn").attr("data-last"));
		var first_ = Number($("#prevPageBtn").attr("data-first"));
		var total_ = Number($("#nextPageBtn").attr("total"));
		var bt = $("#startDate").val();
		var et = $("#endDate").val();
		var tyepp = $(".noticeTab").attr("data");
		if(tyepp=="1"){
			var vy = 1;
		}else{
			var vy = 2;
		}
			if(tarBtn.hasClass("prevBtn")){
				that.nextBtn.removeClass("disable");
				if(first_=="0"){
					that.prevBtn.addClass("disable");
					return false;
				}
				else{
						qQuery.query({
							last : first_,
							bt : bt,
							et :et,
							action:vy,
							title:title
						})
				}

			}else{
				that.prevBtn.removeClass("disable");
				if(last_<=total_){
						qQuery.query({
							last : last_,
							bt : bt,
							et :et,
							action:vy,
							title:title
						})
				}else{
					that.nextBtn.addClass("disable");
				}
			}
			
	},
	query : function(opt){
		var tyepp = $(".credit_active").attr("data");
		if(tyepp=="1"){
			var vy = 1;
		}else{
			var vy = 0;
		}
		var that = this;
		var aid = opt.id;
		var li_type = opt.li_type || 0;//0代表已发布 移至草稿箱    1代表草稿箱   发布  
		var now = $(".search_titile").attr("now");
		var opt = opt || {};
		var pageSize = opt.pageSize || 1;
		var last = opt.last || "1";
		var currentPage = opt.last || "0";
		var sid = opt.sid;
		var pid = opt.pid;
		var tyo = opt.tyo || "1";
		var action = opt.action || "1";
		var dtype = opt.dtype || "4";
		var bt = opt.bt ;
		var pa = opt.pa || "0"; 
		var et = opt.et || now;
		var data_li = opt.data_li;
		var ttimeout = opt.ttimeout || 20 * 60 * 1000;
		var data_li = $(".pub_top_left").attr("data_li");
		var listUl = $("#mlistUl");
		var title = opt.title;
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		var data_a = {
				//pageSize : pageSize,
				currentPage : last,
				id : aid,
				bt : bt,
				et : et,
				title : title,
				action:action
				
			};
			PFT.Ajax({
				url : "module/pft_announce/announce.php",
				type : "GET",
				dataType : "json",
				data : data_a,
				loading : function(){
					listUl.html(status["loading"]);
					pagenavW.hide();
					},
				removeLoading : function(res){
					that.statics.last = "";
					that.statics.currentPage += 1;
					listUl.html("");
					pagenavW.show();
				}
			},function(res){
				var lists = res.list;
					if(!that.statics.isObjEmpty(lists)){ 
						$(".tbCon").show();
						//console.log(li_type)
						that.buildHtml(res,li_type);
					}else{
						listUl.html(status["empty"]);
						$("#pagenavW").hide();
						$(".tbCon").hide();
					}
					if(res.status=="fail"){
						PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">'+res.msg+'</p>');
					}
			})
	},
	Operation : function(opt){
		var action = opt.action;
		//var that = opt.this;
		var id = opt.id;
		var status = opt.status;
		//var del_type = opt.del_type; //判断是1是 已发布  2是草稿箱的
		var data = {
			id : id,
			action:action,
			status:status
		};
		PFT.Ajax({
				url : "module/pft_announce/announce_add.php",
				//url : "admin/pppp.php",
				type : "GET",
				dataType : "json",
				data : data
			},function(res){
				if(res.status=="success"){
					//$(".noticeTab").attr("compame","1")  //移除dom元素
					PFT_GLOBAL.U.Alert("success",'<p style="width:240px">'+res.msg+'</p>');
					setTimeout(function () {
						if(status=="0"){
							var bt = $("#startDate").val();
							var et = $("#endDate").val();
							qQuery.query({
								bt:bt,
								et:et,
								action:1,
								li_type : 0
							})
							$(".published").addClass("active");
							$(".draft").removeClass("active");
						}else{
							var bt = $("#startDate").val();
							var et = $("#endDate").val();
							qQuery.query({
								bt:bt,
								et:et,
								action:2,
								li_type : 1
							 })
							$(".draft").addClass("active");
							$(".published").removeClass("active");
							$(".pub_top_left").attr("data_li","0");
						}
					
						//location.reload();
					}, 1000);

				}else{
					PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">'+res.msg+'</p>');
					
				}
			})
		
		
	},
	del_query : function(opt){
		var action = opt.action;
		//var that = opt.this;
		var id = opt.id;
		var status = opt.status;
		var del_type = opt.del_type; //判断是1是 已发布  2是草稿箱的
		var data = {
			id : id,
			action:action,
			status:status
		};
		PFT.Ajax({
				url : "module/pft_announce/announce_add.php",
				//url : "admin/pppp.php",
				type : "GET",
				dataType : "json",
				data : data
			},function(res){
				if(res.status=="success"){
					//$(".noticeTab").attr("compame","1")  //移除dom元素
					PFT_GLOBAL.U.Alert("success",'<p style="width:240px">'+res.msg+'</p>');
					setTimeout(function () {
						if(del_type=="1"){
							var bt = $("#startDate").val();
							var et = $("#endDate").val();
							qQuery.query({
								bt:bt,
								et:et,
								action:1,
								li_type : 0
							})
							$(".published").addClass("active");
							$(".draft").removeClass("active");
						}else{
							var bt = $("#startDate").val();
							var et = $("#endDate").val();
							qQuery.query({
								bt:bt,
								et:et,
								action:2,
								li_type : 1
							 })
							$(".draft").addClass("active");
							$(".published").removeClass("active");
							$(".pub_top_left").attr("data_li","0");
						}
					
						//location.reload();
					}, 1000);

				}else{
					PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">'+res.msg+'</p>');
					
				}
			})
		
		
	},
	buildHtml : function(res,li_type){
		var that = this;
		var lists = res.list;
		var li_type = li_type;
		var str = "";
		var last = res.last;
		var data = lists;
		var total = res.total;
		var totalPage = res.totalPage || "0";
		var currentPage = res.currentPage;
		$(".nextBtn").attr("total",totalPage);
		$("#prevPageBtn").attr("data-mid",currentPage);
		$("#prevPageBtn").attr("data-last",currentPage+1);
		$("#prevPageBtn").attr("data-first",currentPage-1);
		$(".search_titile").attr("time",totalPage)
		str +=   '<thead>';
		str +=   '	<tr>';
		str +=   '		<th class="setAlign">标题</th>';
		str +=   '		<th class="w80"><div class="iconPing"></div></th>';
		//str +=   '		<th class="w80"><div class="iconZan"></div></th>';
		str +=   '		<th class="w110">发布时间</th>';
		str +=   '		<th class="w210">其他操作</th>';
		str +=   '	</tr>';
		str +=   '</thead>';
		str +=   '<tbody class="tbody">';
		for(var i in data){
			str +=   '<tr>';
			str +=   '	<td class="colorBlue setAlign">'+data[i]["title"]+'</td>';
			//str +=   '	<td>'+data[i]["thumbs"]+'条</td>';
			str +=   '	<td>'+data[i]["remarks"]+'</td>';
			str +=   '	<td>'+data[i]["create_time"]+'</td>';
			str +=   '	<td>';
			str +=   '		<span class="editList" id="'+data[i]["id"]+'" onclick=\"javascript:window.open(\'http://www.12301.cc/pft_announce.html?id='+data[i]["id"]+'&m=con\')\">查看</span>';
			str +=   '		<span class="editList" id="'+data[i]["id"]+'" onclick=\"javascript:window.open(\'http://www.12301.cc/pft_announce.html?id='+data[i]["id"]+'&m=announce\')\">编辑</span>';
			if(li_type=="0"){
				str +=   '		<span class="editList drafts_li" id="'+data[i]["id"]+'">移至草稿箱</span>';
			}else{
				str +=   '		<span class="editList drafts_li" id="'+data[i]["id"]+'">发布</span>';
			}
			str +=   '		<span class="editList del_li" id="'+data[i]["id"]+'">删除</span>';
			str +=   '	</td>';
			str +=   '</tr>';
		}
		str +='</tbody>';
		$(".noticeTb").html(str);
		$("html,body").animate({"scrollTop":0})
		$("#total_num").text("共"+totalPage+"页");	
		$("#whichPageNum").text(currentPage);
	}
});
var qQuery = new Query({container:$("#notice_")});
var Main = RichBase.extend({
	statics : {},
	EVENTS : {
		"click" : {
			".published" : "publish_list",
			".draft" : "draft_list",
			".page_sub" : "bl",
			".drafts_li" : "drafts",
			".del_li" : "del_li"
		}},
	init : function(opt){
		var that = this;
		this.container = opt.container;
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
		var bt = $("#startDate").val();
		var et = $("#endDate").val();
		qQuery.query({
			 bt:bt,
			 et:et
		});
	},
	drafts : function(that,e){
		var typ = $(".noticeTab").attr("data");  //判断是1是 已发布  2是草稿箱的
		if(typ=="1"){
			var status = "0";
		}else{
			var status = "1";
		}
		var target = $(e.currentTarget);
		var id =target.attr("id");
		qQuery.Operation({
				id : id,
				action : "drafts",
				status : status
			});
		// setTimeout(function () {   //移除dom元素
			// var compame = $(".noticeTab").attr("compame")
			// if(compame=="1"){
				// target.parent().parent().remove()
				// $(".noticeTab").attr("compame","0")
			// }
			
		// }, 1000);
		
		
	},
	del_li : function(that,e){
		var typ = $(".noticeTab").attr("data"); //判断是1是 已发布  2是草稿箱的
		var target = $(e.currentTarget);
		var id =target.attr("id");
		if(typ=="1"){    //已发布的删除
			qQuery.del_query({
				id : id,
				action : "delete",
				status : status,
				del_type : 1
			});
		}else{       //草稿的删除
			qQuery.del_query({
				id : id,
				action : "delete",
				status : status,
				del_type : 2
			});
			
		}
		
		
		
		
	},
	bl : function(){
		var page_size = Number($(".page_num_").val());
		var all = Number($(".nextBtn").attr("total"));
		var bt = $("#startDate").val();
		var et = $("#endDate").val();
		var action = $(".noticeTab").attr("data");
		var title = $(".searchInp").val();
		var data_li = $(".pub_top_left").attr("data_li");
		var li_type = $(".noticeTab").attr("li_type");
		if(page_size<=all){
					qQuery.query({
						bt : bt,
						et : et,
						last:page_size,
						action:action,
						title : title,
						li_type : li_type
					});
			
		}
		else{
			return false;
		}
		$(".page_num_").val("");
		
	},
	publish_list : function(that,e){
			var target = $(e.currentTarget);
			var bt = $("#startDate").val();
			var et = $("#endDate").val();
			$(".noticeTab").attr("data","1");
			$(".noticeTab").attr("li_type","0");
			$(".pub_top_left").attr("data_li","2");
				qQuery.query({
					bt:bt,
					et:et,
					action:1,
					li_type : 0
				})
			target.addClass("active");
			$(".draft").removeClass("active");
			var pa = Number($(".pub_top_left").attr("data_b"));	
	},
	draft_list : function(that,e){
			var target = $(e.currentTarget);
			var bt = $("#startDate").val();
			var et = $("#endDate").val();
			$(".noticeTab").attr("data","2");
			$(".noticeTab").attr("li_type","1");
			$(".pub_top_left").attr("data_li","0");
			qQuery.query({
				bt:bt,
				et:et,
				action:2,
				li_type : 1
			 })
			target.addClass("active");
			$(".published").removeClass("active");
			$(".pub_top_left").attr("data_li","0");
			var pa = Number($(".pub_top_left").attr("data_a"));

	}

});
var search = RichBase.extend({
	statics : {},
	EVENTS : {
		"click" : {
			".searchBtn" : "search_sub",
			".del_x" : "del"
			}},
	init : function(opt){
		var that = this;
	},
	search_sub :function(){ 
		var bt = $("#startDate").val();
		var et = $("#endDate").val();
		var title = $(".searchInp").val();
		//if(!title){
		//	return false;
		//}
		var action = $(".noticeTab").attr("data");
		if(!bt || !et){
			PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">时间设定有误</p>');
			return false
		}else{
				qQuery.query({
					action:action,
					bt : bt,
					et : et,
					title : title
				});
		}
	},
	del : function(){
		$(".searchInp").val("");
		var bt = $("#startDate").val();
		var et = $("#endDate").val();
		var title = $(".searchInp").val();
		var action = $(".noticeTab").attr("data");
		qQuery.query({
			action:action,
			bt : bt,
			et : et,
			title : title
		});
		
		
		
		
	}
});
new Main({container:$("#notice_")});
new search({container:$("#notice_")});



